*******************************************************
* Table 2: Regressions on ASER Scores
*******************************************************
di "ASER Scores, Ordered Logit, Panel A (without controls) + Panel B (with controls)"
preserve

*-------------------------
*PANEL A: Without any controls
*-------------------------

* Set 1: Standardized Test Scores
est clear
foreach var in $baseoutcomes_std {
	
*	local var = "readcode_std"
	di "`var' without controls"
	*1) Without any controls
*	local var "readcode"
	gen Baseline = `var'
	la var Baseline "Baseline"
*	gen miss = missing(base)
*	replace base = 0 if missing(base)
	gen miss = 0


	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg el_`var' treatment Baseline miss, vce(robust)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panela_`var': qui bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg el_`var' treatment Baseline miss
*	eststo: reg `var'_el treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ el_`var' if treatment == 0 & miss == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop Baseline miss temp
}

* Set 2: ORDERED LOGIT REGRESSIONS
foreach var in $baseoutcomes {
	di "`var' without controls, ordered logit"
	gen Baseline = `var'
	la var Baseline "Baseline"
*	gen miss = missing(base)
*	replace base = 0 if missing(base)
	gen miss = 0
	
	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	 ologit el_`var' treatment Baseline miss
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panela_`var': qui bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui ologit el_`var' treatment Baseline miss
*	eststo: qui ologit `var'_el treatment base miss
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ el_`var' if treatment == 0 & miss == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "O.logit", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp
	qui estadd scalar pvalri = r(mean)
	drop Baseline miss temp
}

*----------------------------------------
*PANEL B: With individual and school controls
*----------------------------------------
* Set 1: Level increases
* Set 1: Standardized Test Scores
foreach var in $baseoutcomes_std {

	di "`var' with controls"
	*1) Without any controls
*	local var "readcode"
	gen Baseline = `var'
	la var Baseline "Baseline"
*	gen miss = missing(base)
*	replace base = 0 if missing(base)
	gen miss = 0

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg el_`var' treatment Baseline miss  $indcontrols $schcontrols, vce(robust)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panelb_`var': qui bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg el_`var' treatment Baseline miss  $indcontrols $schcontrols
*	eststo: reg el_`var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ el_`var' if treatment == 0 & miss == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "Yes", replace
	qui estadd local sfe = "Yes", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop Baseline miss temp
}

* Set 2: ORDERED LOGIT REGRESSIONS
foreach var in $baseoutcomes {
	di "`var' with controls, ordered logit"
	gen Baseline = `var'
	la var Baseline "Baseline"
*	gen miss = missing(base)
*	replace base = 0 if missing(base)
	gen miss = 0
	
	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	ologit el_`var' treatment Baseline miss  $indcontrols $schcontrols
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panelb_`var': qui bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui ologit el_`var' treatment Baseline miss $indcontrols $schcontrols
*	eststo: qui ologit el_`var' treatment Baseline miss
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ el_`var' if treatment == 0 & miss == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "Yes", replace
	qui estadd local sfe = "Yes", replace
	qui estadd local regtype = "O.logit", replace
	qui estadd scalar rsq = e(r2)

	qui summ temp
	qui estadd scalar pvalri = r(mean)
	drop Baseline miss temp
}


# delimit ;
esttab  panela* using "$tables/Table2.tex", replace
keep(treatment Baseline)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Standardized ASER Scores" "ASER Score", pattern(1 0 1 0) ///
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
mtitle("Read" "Maths" "Read" "Maths") ///
posthead("\midrule \multicolumn{5}{l}{\textit{\textbf{Panel A. Without individual and school controls}}} \\ \midrule")
postfoot("")
stats(pvalreg pvalri rsq, labels("p-val (OLS)" "p-val (RI)" "\midrule R2") ///
fmt(2 2 2)) ///
collabels(none) label booktabs nonotes
;

# delimit ;
esttab  panelb* using "$tables/Table2.tex", append
keep(treatment Baseline) ///
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par)) /// 
starlevels(* .10 ** .05 *** .01) ///
prehead("") ///
posthead("\midrule \multicolumn{5}{l}{\textit{\textbf{Panel B. With individual and school controls}}} \\ \midrule")
stats(pvalreg pvalri rsq N regtype, labels("p-val (OLS)" "p-val (RI)" "\midrule R2" "N" "\midrule Reg. Type") ///
fmt(2 2 2 0)) ///
collabels(none) mlabels(none) nonumbers label booktabs nonotes nomtitles;
#delimit cr	

restore 
